package com.no1ikun.view;

import com.no1ikun.publicdb.Authority;
import com.no1ikun.publicdb.DB;
import com.no1ikun.service.TeacherService;
import com.no1ikun.service.TeacherServiceImpl;
import com.no1ikun.util.MyButton;
import com.no1ikun.util.MyJBox;
import com.no1ikun.util.MyJLabel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;

public class MyJDialog extends JDialog {
    private String title;
    private int width;
    private int height;
    private MyJTextField nameEdit;
    private MyJTextField passwordEdit;
    private MyJBox sexEdit;
    private MyJTextField authorityEdit;
    private MyJTextField earth_Edit;
    private MyJBox cno_Edit;

    private TeacherService teacher = new TeacherServiceImpl();
    public MyJDialog(String title, int width, int height) {
        this.title = title;
        this.width = width;
        this.height = height;

        init();
    }

    private void init() {
        //1.设置标题
        this.setTitle(title);
        //要将窗口显示在屏幕的正中间:思路,先获取屏幕的宽高,让后再计算就可以
        //获取屏幕宽高
        double pmwidth = Toolkit.getDefaultToolkit().getScreenSize().getWidth();//获得屏幕的宽
        double pmheight = Toolkit.getDefaultToolkit().getScreenSize().getHeight();//获取屏幕的高
        //2.设置宽高以及显示的位置,将窗体居中显示this.getWidth():获取窗体的宽,this.getHeight()获取屏幕的高
        this.setBounds((int) (pmwidth - width) / 2, (int) (pmheight - height) / 2, width, height);
        //给窗口一个布局,绝对布局
        this.setLayout(null);
        //3.添加一个背景图片
        URL resource = LoginView.class.getResource("../img/bg4.png");
        ImageIcon imageIcon = new ImageIcon(resource);
        JLabel bglable = new JLabel();
        bglable.setBounds(0, 0, 530, 600);
        bglable.setIcon(imageIcon);

        //4.添加项目图标
        URL resource2 = LoginView.class.getResource("../img/icon.png");
        ImageIcon iconimg = new ImageIcon(resource2);
        Image image = iconimg.getImage();


        //添加文字
        MyJLabel maxText = new MyJLabel("老师注册信息", 200, 40, 170, 40, "微软雅黑", 1, 30, Color.WHITE);
        MyJLabel nameText = new MyJLabel("姓名", 40, 25, 110, 100 + 40, "微软雅黑", 1, 17, Color.WHITE);
        MyJLabel passwordText = new MyJLabel("密码", 40, 25, 110, 160 + 40, "微软雅黑", 1, 17, Color.WHITE);
        MyJLabel sexText = new MyJLabel("性别", 40, 25, 110, 220 + 40, "微软雅黑", 1, 17, Color.WHITE);
        MyJLabel authority = new MyJLabel("授权码", 60, 25, 90, 280 + 40, "微软雅黑", 1, 17, Color.WHITE);
//        MyJLabel earth_place = new MyJLabel("生源地", 60, 25, 95, 320 + 40, "微软雅黑", 1, 17, Color.WHITE);
//        MyJLabel cno = new MyJLabel("班级", 40, 25, 110, 380 + 40, "微软雅黑", 1, 17, Color.WHITE);

        //添加编辑框
        nameEdit = new MyJTextField(160, 140, 235, 25);
        passwordEdit = new MyJTextField(160, 200, 235, 25);
        sexEdit = new MyJBox(new String[]{"男","女"},160, 260, 235, 25,Color.white,Color.black);
        authorityEdit = new MyJTextField(160, 320, 235, 25);
//        earth_Edit = new MyJTextField(160, 360, 235, 25);
//        cno_Edit = new MyJBox(new String[]{"一班","二班","三班","四班"},160, 420, 235, 25,Color.white,Color.black);

        //添加两个按钮
        MyButton insert = new MyButton("添加", 180, 400, 70, 25, Color.WHITE, new Color(28, 136, 201));
        MyButton reset = new MyButton("重置", 300, 400, 70, 25, Color.WHITE, new Color(28, 136, 201));
        this.add(insert);
        this.add(reset);

        this.add(nameEdit);
        this.add(passwordEdit);
        this.add(sexEdit);
//        this.add(earth_Edit);
        this.add(authorityEdit);
//        this.add(cno_Edit);

        this.add(nameText);
        this.add(passwordText);
        this.add(sexText);
//        this.add(earth_place);
        this.add(authority);
//        this.add(cno);
        this.add(maxText);
        this.add(bglable);
        this.setIconImage(image);


        //显示对话框
        this.setVisible(true);

        //重置功能完成监听事件
        reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                //将所有控件的内容重置就可以了
                nameEdit.setText("");
                passwordEdit.setText("");
//                sexEdit.setText("");
//                earth_Edit.setText("");
//                birthdayEdit.setText("2000-01-01");
            }
        });

        //注册功能
        //思路:1.获取前端提交过来的数据,去数据库添加数据,返回结果判断,给前端完成显示操作

        insert.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //注册按钮的点击事件
                //获取前端的数据
                String name = nameEdit.getText();
                String password = passwordEdit.getText();
                String sex = sexEdit.getText();
                String authorityText = authorityEdit.getText();
//                String earth_place = earth_Edit.getText();
//                int Cno=cno_Edit.getIndex()+1;

                //判断用户输入的数据是否是空的
                if(!name.equals("")&&!password.equals("")){
                    //判断输入是否合法
                    //字符串对正则的支持
                    if(!authorityText.equals(Authority.REGISTRY_KEY)){
                        JOptionPane.showMessageDialog(null, "需要授权认证码！请质询教务处获取后重试！", "温馨提示", JOptionPane.WARNING_MESSAGE);
                    }else{
                        //通过验证 要去数据库添加数据
                        // todo 用户主键
                        int i =   teacher.insertTeacher(name,password);

                        if(i>0){
                            JOptionPane.showMessageDialog(null, "工号为："+TeacherServiceImpl.Tid, "温馨提示", JOptionPane.WARNING_MESSAGE);
                            //关闭自己,跳转到登录界面
                            setVisible(false);
                            //显示登录界面
                            DB.loginView.setVisible(true);
                        }
                    }
                }else{

                    JOptionPane.showMessageDialog(null, "注册信息不能为空!", "温馨提示", JOptionPane.WARNING_MESSAGE);
                }



            }
        });
        //监听关闭窗口事件
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                int option = JOptionPane.showConfirmDialog(null, "确定要退出注册数吗?", "温馨提示", JOptionPane.YES_NO_OPTION);
                //判断用户选择的是哪个按钮
                if (option == JOptionPane.YES_OPTION) {
                    //将登录界面显示出来
                    DB.loginView.setVisible(true);
                    //将自己隐藏
                    setVisible(false);
                } else {
                    //不做任何处理
                    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
                }
            }
        });
    }


}
